import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'orderSplitM',
  state: {},
  effects: {
    //根据产品组查询组合树
    *queryPortfolioList({ payload, callback }, { call, put }) {
      const res = yield call(api.orderSplit.queryPortfolioList, payload);
      if (res && res.code == 0) {
        const data = res?.data?.dataSource?.length ? res.data.dataSource[0].children : [];
        callback(data);
      } else {
        callback();
        message.error(res ? res.msg || '查询组合树失败' : '查询组合树失败');
      }
    },
    //批量查询 持仓
    *queryPositongList({ payload, callback }, { call, put }) {
      const res = yield call(api.orderSplit.queryPositongList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询可用持仓失败' : '查询可用持仓失败');
      }
    },
    //批量查询 资金
    *queryFundList({ payload, callback }, { call, put }) {
      const res = yield call(api.orderSplit.queryFundList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询可用资金失败' : '查询可用资金失败');
      }
    },
    //拆分保存
    *splitSave1({ payload, callback }, { call, put }) {
      const response = yield call(api.orderSplit.splitSave1, payload);
      if (response && response.code == 0) {
        callback('success');
        message.success('拆分成功');
      } else {
        callback();
        message.error(response ? response.msg || '拆分失败' : '拆分失败');
      }
    },
    // //拆分保存
    *splitSave2({ payload, callback }, { call, put }) {
      const response = yield call(api.orderSplit.splitSave2, payload);
      if (response && response.code == 0) {
        callback('success');
        message.success('拆分成功');
      } else {
        callback();
        message.error(response ? response.msg || '拆分失败' : '拆分失败');
      }
    },
    // //查询详情理财直融、债券、非标）
    *querySplitTableList1({ payload, callback }, { call, put }) {
      const response = yield call(api.orderSplit.querySplitTableList1, payload);
      if (response && response.code == 0) {
        response.data &&
          response.data.map((item, index) => {
            item.key = index;
          });
        callback(response.data);
      } else {
        callback();
        message.error(response ? response.msg || '查询详情失败' : '查询详情失败');
      }
    },
    // //查询详情回购
    *querySplitTableList2({ payload, callback }, { call, put }) {
      const response = yield call(api.orderSplit.querySplitTableList2, payload);
      if (response && response.code == 0) {
        response.data &&
          response.data.map((item, index) => {
            item.key = index;
            item.isEdit = false;
          });
        callback(response.data);
      } else {
        callback();
        message.error(response ? response.msg || '查询详情失败' : '查询详情失败');
      }
    },
    //查询每个组合的质押券
    *queryHold({ payload, callback }, { call, put }) {
      const response = yield call(api.interBank.getPositionData, payload);
      if (response && response.code == 0) {
        const dataSource = response?.data?.dataSource
          ? response.data.dataSource?.map((item, key) => {
              item.key = key;
              item.name = item.pledgeBondCode + '-' + item.pledgeBondName;
              item.exchangecode = item.exchangecode;
              item.tradingcode = item.secucode;
              item.tradePrice = 0;
              item.tradeNum = 0;
              return item;
            })
          : [];

        callback(dataSource);
      } else {
        callback();
        message.error(response ? response.msg || '查询质押券失败' : '查询质押券失败');
      }
    },
    //融资回购  拆分查询表格
    *getBuyBackInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.orderSplit.getBuyBackInfo, payload);
      if (response && response.code == 0) {
        response.data &&
          response.data.map((item, index) => {
            item.key = index;
            item.isEdit = false;
          });
        callback(response.data);
      } else {
        callback();
        message.error(response ? response.msg || '查询产品失败' : '查询产品失败');
      }
    },
    reducers: {
      save(state, { payload }) {
        return {
          ...state,
          ...payload,
        };
      },
    },
  },
};
